#include "user_mqtt.h"

static const char *TAG = "MQTT_EXAMPLE";

esp_mqtt_client_handle_t client;

char mqtt_command[128];

void Received_Command_Process(char *receive_data)
{
    char *token;

    ESP_LOGI(TAG,"Receive Cmd:%s\r\n",receive_data);
    token=strstr(receive_data,"MODE&");
    if(token!=NULL)
    {
        printf("%s\n",token);
        token=strtok(token,",");
        while(token!=NULL)
        {            
            char temp[16]={""};

            if(!strncmp(token,"MODE",4)){//阿里云指令：MODE&WORK,OUT&0,。
                strcpy(msg,token+5);
                
                ESP_LOGI(TAG, "Device Operating Mode:%s\r\n",msg);
            }   

            if(!strncmp(token,"OUT",3)){
                strcpy(temp,token+4);
                sscanf(temp,"%d",&timemsg[0]); 
                ESP_LOGI(TAG, "OUT_PORT_DATA:%d\r\n",timemsg[0]);
                out_set_flag=1;           
            }

            token=strtok(NULL,",");
        }
    }   
}
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{

    switch (event->event_id) {
        case MQTT_EVENT_CONNECTED://MQTT连接事件
            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
            esp_mqtt_client_publish(client, MQTT_DEVICE_DATA, "IO_Board_Device", 0, 0, 0);//上报设备类型
            esp_mqtt_client_subscribe(client, MQTT_USER_COMMAND, 0);//订阅命令主题

            break;
        case MQTT_EVENT_DISCONNECTED:
            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
            break;

        case MQTT_EVENT_SUBSCRIBED://MQTT订阅事件
            ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);

            break;
        case MQTT_EVENT_UNSUBSCRIBED:
            ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
            break;
        case MQTT_EVENT_PUBLISHED:
            ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
            break;
        case MQTT_EVENT_DATA:

            if(!strncmp(MQTT_USER_COMMAND,event->topic, event->topic_len))
            {
                strncpy((char *)mqtt_command, event->data, event->data_len);
                ESP_LOGI(TAG, "MQTT_COMMAND:%s\r\n",mqtt_command);
                Received_Command_Process(mqtt_command);
            }

            ESP_LOGI(TAG, "MQTT_EVENT_DATA");
            ESP_LOGI(TAG, "TOPIC=%.*s", event->topic_len, event->topic);
         	ESP_LOGI(TAG, "DATA=%.*s", event->data_len, event->data);
			
            break;
        case MQTT_EVENT_ERROR:
            ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
            break;
        default:
            ESP_LOGI(TAG, "Other event id:%d", event->event_id);
            break;
    }
    return ESP_OK;
}

static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
    mqtt_event_handler_cb(event_data);
}

void user_mqtt_app_start(void)
{
    esp_mqtt_client_config_t mqtt_cfg = {
		.host = Aliyun_host,
		.port = Aliyun_port,
		.client_id = Aliyun_client_id,
		.username = Aliyun_username,
		.password = Aliyun_password

    };
    client = esp_mqtt_client_init(&mqtt_cfg);
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
    esp_mqtt_client_start(client);

}

